package com.empire.module.member.service.warnconfigpersonrelation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.empire.module.member.dal.dataobject.casepersonrelation.CasePersonRelationDO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.empire.module.member.controller.admin.warnconfigpersonrelation.vo.*;
import com.empire.module.member.dal.dataobject.warnconfigpersonrelation.WarnConfigPersonRelationDO;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.util.object.BeanUtils;

import com.empire.module.member.dal.mysql.warnconfigpersonrelation.WarnConfigPersonRelationMapper;

import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.empire.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.empire.module.member.enums.ErrorCodeConstants.*;

/**
 * 预警条件-人员关系 Service 实现类
 *
 * @author 主席大人
 */
@Service
@Validated
public class WarnConfigPersonRelationServiceImpl implements WarnConfigPersonRelationService {

    @Resource
    private WarnConfigPersonRelationMapper warnConfigPersonRelationMapper;

    @Override
    public Set<Long> getWarnConfigPersonRelationByWarnConfigId(String warnConfigId) {
        LambdaQueryWrapper<WarnConfigPersonRelationDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WarnConfigPersonRelationDO::getWarnConfigId, warnConfigId);
        List<WarnConfigPersonRelationDO> relationDOList = warnConfigPersonRelationMapper.selectList(wrapper);
        return relationDOList.stream().map(WarnConfigPersonRelationDO::getPersonId).collect(Collectors.toSet());
    }

    @Override
    public Integer createWarnConfigPersonRelation(WarnConfigPersonRelationSaveReqVO createReqVO) {

        // 插入新的关系
        List<WarnConfigPersonRelationDO> relationList = new ArrayList<>();
        List<Long> personIdList = createReqVO.getPersonIdList();
        for (Long personId : personIdList) {
            WarnConfigPersonRelationDO warnConfigPersonRelationDO = new WarnConfigPersonRelationDO();
            warnConfigPersonRelationDO.setWarnConfigId(createReqVO.getWarnConfigId());
            warnConfigPersonRelationDO.setPersonId(personId);
            warnConfigPersonRelationDO.setCreateTime(LocalDateTime.now());
            relationList.add(warnConfigPersonRelationDO);
        }
        warnConfigPersonRelationMapper.insertBatch(relationList);
        // 返回
        return relationList.size();
    }

    @Override
    public void updateWarnConfigPersonRelation(WarnConfigPersonRelationSaveReqVO updateReqVO) {
        // 校验存在
        validateWarnConfigPersonRelationExists(updateReqVO.getId());
        // 更新
        WarnConfigPersonRelationDO updateObj = BeanUtils.toBean(updateReqVO, WarnConfigPersonRelationDO.class);
        warnConfigPersonRelationMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarnConfigPersonRelation(String warnConfigId, Long personId) {
        LambdaQueryWrapper<WarnConfigPersonRelationDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WarnConfigPersonRelationDO::getWarnConfigId, warnConfigId);
        queryWrapper.eq(WarnConfigPersonRelationDO::getPersonId, personId);
        warnConfigPersonRelationMapper.delete(queryWrapper);
    }

    private void validateWarnConfigPersonRelationExists(Long id) {
        if (warnConfigPersonRelationMapper.selectById(id) == null) {
            throw exception(WARN_CONFIG_PERSON_RELATION_NOT_EXISTS);
        }
    }

    @Override
    public WarnConfigPersonRelationDO getWarnConfigPersonRelation(Long id) {
        return warnConfigPersonRelationMapper.selectById(id);
    }

    @Override
    public PageResult<WarnConfigPersonRelationDO> getWarnConfigPersonRelationPage(WarnConfigPersonRelationPageReqVO pageReqVO) {
        return warnConfigPersonRelationMapper.selectPage(pageReqVO);
    }

}